package com.network.retrofit_rx.Api;


import android.util.Log;
import android.widget.Toast;

import com.network.utils.NetworkUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;


/**
 * @author QiQi
 */
public class ActionSubscriber<T> implements Observer<T> {


    private final static String TAG = ActionSubscriber.class.getSimpleName();

    private Disposable mDisposable;

    private Consumer<? super T> onNext;
    private Consumer<Throwable> onError;
    private Action onCompleted;

    private Observer<T> observer;

    public ActionSubscriber(Consumer<? super T> onNext, Consumer<Throwable> onError, Action onCompleted) {
        this.onNext = onNext;
        this.onError = onError;
        this.onCompleted = onCompleted;
    }

    public ActionSubscriber(Observer<T> observer) {
        this.observer = observer;
    }


    @Override
    public void onSubscribe(Disposable d) {
        mDisposable = d;
        if (observer != null) {
            observer.onSubscribe(d);
        }
    }

    @Override
    public void onNext(T t) {
        if (observer != null) {
            observer.onNext(t);
        }

        if (onNext != null) {
            try {
                onNext.accept(t);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable throwable) {
        if (observer != null) {
            observer.onError(throwable);
        }


        try {
            if (onError != null) {
                onError.accept(throwable);
            }

            if (throwable instanceof UnknownHostException || throwable instanceof ConnectException) {
                showToast("网络连接出错,请检查网络");
            } else if (throwable instanceof TimeoutException || throwable instanceof SocketTimeoutException) {
                showToast("网络请求超时, 请稍候重试");
            } else {
                Log.d(TAG, throwable.getMessage());
                showToast(throwable.getMessage());
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (!mDisposable.isDisposed()) {
                mDisposable.dispose();
            }
        }
    }

    private void showToast(String msg) {
        Toast.makeText(NetworkUtils.appContext, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onComplete() {
        if (observer != null) {
            observer.onComplete();
        }

        try {
            if (onCompleted != null) {
                onCompleted.run();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (!mDisposable.isDisposed()) {
                mDisposable.dispose();
            }
        }
    }


    public void unSubscribe() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }

    public Disposable getDisposable() {
        return mDisposable;
    }

}
